package com.css.fxfzfxqh.modules.singleIndexRiskZoning.controller;

import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.modules.singleIndexRiskZoning.model.entity.Assess;
import com.css.fxfzfxqh.modules.singleIndexRiskZoning.model.entity.FxqhAssessEdQh;
import com.css.fxfzfxqh.modules.singleIndexRiskZoning.model.entity.FxqhAssessEdQhResult;
import com.css.fxfzfxqh.modules.singleIndexRiskZoning.model.vo.FxqhAssessEdQhResultVo;
import com.css.fxfzfxqh.modules.singleIndexRiskZoning.service.EnterAssessQhService;
import com.css.fxfzfxqh.modules.singleIndexRiskZoning.vo.GdpVO;
import com.css.fxfzfxqh.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 确定评估风险区划接口
 */
@Slf4j
@RestController
@RequestMapping("enterAssessQh")
public class EnterAssessQhController {

    @Autowired
    private EnterAssessQhService enterAssessQhService;

    /**
     * 获取区划记录列表
     *
     * @param param
     * @return
     */
    @GetMapping("/getAssessPage")
    public RestResponse getAssessPage(HttpServletRequest request, @RequestParam Map<String, Object> param) {
        RestResponse restResponse = null;
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> pageMap = enterAssessQhService.getAssessPage(param, curPage, pageSize);
            return RestResponse.succeed(pageMap);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    /**
     * 生成区划记录编码
     *
     * @param
     * @return
     */
    @GetMapping("/getCode")
    public RestResponse getCode() {
        RestResponse restResponse = null;
        try {
            String code = enterAssessQhService.getCode();
            return RestResponse.succeed(code);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    /**
     * 根据当前登录人获取省id
     *
     * @return
     */
    @GetMapping("/getAreaByUserId")
    public RestResponse getAreaByUserId() {
        RestResponse restResponse = null;
        try {
            String list = enterAssessQhService.getAreaByUserId();
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    /**
     * 根据条件获取评估记录
     *
     * @param param estimateObj：1、抗震能力；2、人员伤亡；3、经济损失
     *              houseDataFlag：0、房屋单体数据；1、房屋网格数据
     *              seismicInputMode：0、烈度；1、4个概率水准；2、地震影响场
     *              provinceName：省名称
     *              cityName：市名称
     *              countyName：县名称
     * @return
     */
    @GetMapping("/getAssessByParam")
    public RestResponse getAssessByParam(@RequestParam Map<String, Object> param) {
        RestResponse restResponse = null;
        try {
            List<Assess> list = enterAssessQhService.getAssessByParam(param);
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    /**
     * 保存区划记录信息
     *
     * @return
     */
    @PostMapping("/saveFxqhAssessEd")
    public RestResponse saveFxqhAssessEd(@RequestBody FxqhAssessEdQh entity) {
        RestResponse restResponse = null;
        try {
            restResponse = enterAssessQhService.saveFxqhAssessEd(entity);
            restResponse.setMessage("保存成功！");
        } catch (Exception e) {
            String errorMessage = "保存失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    /**
     * 根据id保获取区划记录信息
     *
     * @return
     */
    @GetMapping("/getById")
    public RestResponse getById(@RequestParam String faeId) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(enterAssessQhService.getById(faeId));
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    /**
     * 修改区划记录信息
     *
     * @return
     */
    @PostMapping("/updateFxqhAssessEd")
    public RestResponse updateFxqhAssessEd(@RequestBody FxqhAssessEdQh entity) {
        RestResponse restResponse = null;
        try {
            restResponse = enterAssessQhService.updateFxqhAssessEd(entity);
            restResponse.setMessage("修改成功！");
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

//    /**
//     * 区划计算
//     * @param param
//     * @return
//     */
//    @PostMapping("/doZoning")
//    public RestResponse doZoning(@RequestBody Map<String,Object> param){
//        RestResponse restResponse = null;
//        try {
//            restResponse= enterAssessQhService.doZoning(param);
//        } catch (Exception e) {
//            String errorMessage = "计算失败!";
//            log.error(errorMessage, e);
//            restResponse = RestResponse.fail(errorMessage);
//        }
//        return restResponse;
//    }

    /**
     * 区划计算
     *
     * @param taskId
     * @return
     */
    @RequestMapping("/doZoning")
    public RestResponse doZoning(@RequestParam String taskId,@RequestParam String selectedVal) throws Exception{
            log.debug("/doZoning,param:{}", taskId);
            return enterAssessQhService.doZoning(taskId,selectedVal);
    }

    /**
     * 查询区划值列表
     *
     * @param param
     * @return
     */
    @GetMapping("getAssessEdQhList")
    public RestResponse<List<FxqhAssessEdQhResultVo>> getAssessEdQhList(FxqhAssessEdQhResult param, Integer page, Integer rows) {
        try {
            List<FxqhAssessEdQhResultVo> list = enterAssessQhService.getAssessEdQhList(param, page, rows);
            return RestResponse.succeed(list);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 模板导出
     */
    @PostMapping("downLoad")
    public void downLoad(@RequestBody List<GdpVO> dbpList, HttpServletResponse response){
        enterAssessQhService.downLoad(dbpList,response);
    }
    /**
     * 数据导出
     */
    @PostMapping("downLoadData")
    public void downLoadData(HttpServletResponse response,@RequestBody  Map<String,String> map){
        enterAssessQhService.downLoadData(map.get("taskId"),response);
    }

    /**
     * 导入
     *
     * @param req
     * @param taskId   评估记录id
     * @return
     */
    @PostMapping(value="/importExcel")
    public RestResponse reportImport( HttpServletRequest req,String taskId) throws Exception{
            //获取上传到服务器的临时文件对象
            Part part = req.getPart("file");
            //获取临时文件的输入流
            InputStream inputStream = part.getInputStream();
            String str = enterAssessQhService.reportImport(inputStream, taskId);
            return RestResponse.succeed(str);
    }
}
